package panel;

import java.awt.Color;
import java.awt.Component;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import model.ListItem;

/**
 * Provides a custom way of rendering the items of the address bar drop down list. This class leverages the html
 * rendering capabilities of {@link JLabel} objects.
 * @author Shahriar Haque
 *
 */
public class CustomListCellRenderer extends JLabel implements ListCellRenderer {


	private static final long serialVersionUID = 1L;
	
	private static Color selectColor;;
	
	private static URL imgURL;
	
	private static String textStyle = "";
	
	public CustomListCellRenderer(){
		imgURL = CustomListCellRenderer.class.getResource("/media/Lemon Grass/folder.png");
		
		
		selectColor = new Color(207,50,8);

		textStyle += "<html><div style=\"font-family: Tahoma; font-size: large\">@name</div>\n";
		textStyle += "<small style=\"font-family: Tahoma; color: rgb(102, 102, 102)\">@description</small></html>";
		
	}

	/**
	 * Given the selected list item, this class generates the necessary html decorations that show up on the 
	 * autocomplete drop down list of the address bar
	 */
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
		ListItem item = (ListItem) value;
		
		// if highlighted, turn the border black and highlight the item name with the selection color
        if (isSelected) {
        	this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
            setForeground(selectColor);
        } else {
        	this.setBorder(null);
            setForeground(Color.black);
        }
		
    	
        String html = new String(textStyle);
        
        html = html.replaceAll("@name", item.getName());
        html = html.replaceAll("@description", item.getDescription());
        
        
        setText( html );
        
        
       
		setIcon(new ImageIcon(imgURL));
		
		return this;
	}

}
